package it.nola.soccer;

import it.nola.soccer.adapter.ActionAdapter;
import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ActionEdit extends AbstractSoccerActivity {
    private Spinner teamSpinner;
    private EditText idActionText;
    private EditText timeText;
    private EditText descriText;
    private Spinner halfSpinner;
    private ListView griglia;
    private TextView titleFormText;
    private final String[] fromList=new String[]{Const.Actions.KEY_TEAM, Const.Actions.KEY_DESCRI,
	Const.Actions.KEY_MIN, Const.Actions.KEY_HALF };
    private final int[] toList=new int[]{R.id.team_action_old, R.id.descri_old, R.id.min_old, R.id.half_old };

    @Override
    public void cancella(View view) {
	String rowId=idActionText.getText().toString();
	if (!"".equals(rowId)) {
	    dbHelper.cancellaCard(Long.parseLong(rowId));
	    Toast toast=Toast.makeText(this, getString(R.string.action_deleted), Toast.LENGTH_SHORT);
	    toast.show();
	    fillData();
	}
	resetField();
    }

    @Override
    public void chiudi(View view) {
	finish();
    }

    @Override
    protected void fillData() {
	Cursor sc=dbHelper.cercaAction(cronaca);
	startManagingCursor(sc);
	ActionAdapter actionAdapter=new ActionAdapter(this, R.layout.actions_row, sc, fromList, toList);
	griglia.setAdapter(actionAdapter);
    }

    @Override
    protected void performSetContentView() {
	setContentView(R.layout.actions_edit);
    }

    @Override
    protected void performSetTitle() {
	setTitle(getText(R.string.actions));
    }

    /**
     * Inizializza i campi presenti nella View
     */
    @Override
    protected void prepareFields() {
	titleFormText=(TextView) ActionEdit.this.findViewById(R.id.form_action);
	idActionText=(EditText) findViewById(R.id.id_action);
	descriText=(EditText) findViewById(R.id.descri);
	teamSpinner=(Spinner) findViewById(R.id.team_action);
	ArrayAdapter<CharSequence> teamAdapter=new ArrayAdapter<CharSequence>(this,
	    android.R.layout.simple_spinner_item);
	teamAdapter.add(team1Name);
	teamAdapter.add(team2Name);
	teamSpinner.setAdapter(teamAdapter);
	halfSpinner=(Spinner) findViewById(R.id.half);
	ArrayAdapter<CharSequence> halfAdapter=ArrayAdapter.createFromResource(this, R.array.halfs,
	    android.R.layout.simple_spinner_item);
	halfSpinner.setAdapter(halfAdapter);
	timeText=(EditText) findViewById(R.id.min);
	griglia=(ListView) findViewById(R.id.gridview);
	griglia.setOnItemClickListener(new OnItemClickListener() {

	    @Override
	    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Cursor c=ActionEdit.this.dbHelper.recuperaAction(id);
		startManagingCursor(c);
		if (c.moveToFirst()) {
		    ActionEdit.this.titleFormText.setText(getString(R.string.edit_action));
		    ActionEdit.this.descriText.setText(c.getString(c.getColumnIndex(Const.Actions.KEY_DESCRI)));
		    ActionEdit.this.halfSpinner.setSelection(c.getInt(c.getColumnIndex(Const.Actions.KEY_HALF)));
		    ActionEdit.this.idActionText.setText(Long.toString(id));
		    ActionEdit.this.timeText.setText(c.getString(c.getColumnIndex(Const.Actions.KEY_MIN)));
		    teamSpinner.getSelectedItemPosition();
		    ActionEdit.this.teamSpinner.setSelection(c.getInt(c.getColumnIndex(Const.Actions.KEY_TEAM)));
		}
	    }
	});
    }

    @Override
    protected void resetField() {
	halfSpinner.setSelection(0);
	teamSpinner.setSelection(0);
	descriText.setText("");
	idActionText.setText("");
	timeText.setText("");
	titleFormText.setText(getString(R.string.new_action));
    }

    @Override
    protected void saveState() {
	String rowId=idActionText.getText().toString();
	String descri=descriText.getText().toString();
	int team=teamSpinner.getSelectedItemPosition();
	int half=halfSpinner.getSelectedItemPosition();
	String min=timeText.getText().toString();
	if (rowId.equals("")) {
	    dbHelper.insertAction(team, half, min, cronaca, descri);
	}
	else {
	    dbHelper.updateAction(Long.parseLong(rowId), team, half, min, cronaca, descri);
	}
	Toast toast=Toast.makeText(this, getString(R.string.just_saved), Toast.LENGTH_SHORT);
	toast.show();
    }
}